using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControler : MonoBehaviour
{
    private Rigidbody2D rb;
    private Animator animator;
    public float Movespeed;
    private float lastSpeed=1;
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }
    private void FixedUpdate()
    {
        Movement();
    }
    void Movement()
    {

        float InputSpeedX = Input.GetAxis("Horizontal");
        float InputSpeedY = Input.GetAxis("Vertical");
        animator.SetBool("move", InputSpeedX != 0 || InputSpeedY != 0);
        rb.velocity = new Vector2(InputSpeedX * Movespeed, rb.velocity.y);
        rb.velocity = new Vector2(rb.velocity.x, InputSpeedY * Movespeed);
        if (InputSpeedX != 0 || InputSpeedY != 0)
        {
            animator.SetFloat("speed",Math.Abs(InputSpeedX)> Math.Abs(InputSpeedY) ? Math.Abs(InputSpeedX) : Math.Abs(InputSpeedY));
        }
        
        if (lastSpeed*InputSpeedX<0)
        {
            this.transform.localScale = new Vector3(-this.transform.localScale.x, this.transform.localScale.y, this.transform.localScale.z);
        }
        if(InputSpeedX!=0)
        lastSpeed = InputSpeedX;
    }

}
